<?php
namespace app\common\server;

use think\Db;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class Mail
{

    private static $html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
             <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <style type="text/css">
                                .table{font-family:Microsoft YaHei;font-size:18px;color:#333333;border-width: 1px;border-color: #eee;border-collapse: collapse;margin: 0 auto;}
                                .title{border-width: 1px;padding: 8px;border-style: solid;border-color: #eee;background-color: #dedede;}
                                .td{border-width: 1px;padding: 8px;border-style: solid;border-color: #eee;background-color: #ffffff;text-align: center;}
                                .cap{font-size: 24px;color:#B0B0B0;margin: 10px 20px;}
                            </style>
                            </head>
                            <body>
                <table class="table">
                <caption class="cap">每日票房纪录</caption>
                <thead>
                <tr><th class="title">编号</th>
                    <th class="title">电影名</th>
                    <th class="title">累计票房</th>
                    <th class="title">今日票房</th>
                    <th class="title">上映天数</th>
                    <th class="title">豆瓣评分</th>
                </tr>
                </thead>
                <tbody>';


    public function send(){
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //Server settings
            $mail->CharSet ="UTF-8";
            $mail->SMTPDebug = 0;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.qq.com';                           // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = '377837240';                 // SMTP username
            $mail->Password = 'qqoczqrpuzkpbjhi';                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to


            //Recipients
            $mail->setFrom('377837240@qq.com', 'Mailer');
//            $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
            $mail->addAddress('861192771@qq.com');               // Name is optional
//            $mail->addReplyTo('ld@shenzan.com', 'Information');
//            $mail->addCC('cc@example.com');
//            $mail->addBCC('bcc@example.com');

            //附件
//            $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = '每日一报';


            $movie = $this->getData();

            $html = self::$html;

            foreach ($movie as $k=>$v){
                $num = $k+1;
                $html .= '<tr><td class="td">'.$num.'</td><td class="td">'.$v['name'].'</td><td class="td">'.$v['all_ticket'].'</td><td class="td">'.$v['today_ticket'].'</td><td class="td">'.$v['pulish_date'].'</td><td class="td">'.$v['grade'].'</td></tr>';
            }
            $html .= '</tbody></table></body></html>';

            $mail->Body    = $html;
            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';

            $mail->send();
            echo '邮件发送成功';
        } catch (Exception $e) {
            echo '邮件发送失败：', $mail->ErrorInfo;
        }


    }



    public function getData(){
        $list = Db::name('movie')->where('createtime','between',[date('Y-m-d',time()),date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d'),date('Y')))])->select();
        return $list;
    }

}